﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NUnit.Util;
using System.IO;
using Microsoft.VisualStudio.TestTools.Common;
using NUnit.Core;
using System.Diagnostics;
using System.Collections;
using System.Security.Permissions;
using System.Globalization;
using System.Reflection;

namespace NUnitForVSTS.NUnitTest
{
    /// <summary>
    /// Handles the execution of the tests
    /// </summary>
    public class TestLoaderExecutionHandler : MarshalByRefObject, IDisposable
    {
        private TestLoaderExecutionEventDispatcher _testController;

        /// <summary>
        /// Initializes a new instance of the <see cref="TestLoaderExecutionHandler"/> class.
        /// </summary>
        public TestLoaderExecutionHandler()
        {
        }

        /// <summary>
        /// Adds the testrunid to this instance
        /// </summary>
        /// <param name="testRunId">the id of the testrun</param>
        public void RegisterTestRunId(Guid testRunId)
        {
            _testController = new TestLoaderExecutionEventDispatcher(testRunId);
        }

        /// <summary>
        /// Obtains a lifetime service object to control the lifetime policy for this instance.
        /// </summary>
        /// <returns>
        /// An object of type <see cref="T:System.Runtime.Remoting.Lifetime.ILease"/> used to control the lifetime policy for this instance. This is the current lifetime service object for this instance if one exists; otherwise, a new lifetime service object initialized to the value of the <see cref="P:System.Runtime.Remoting.Lifetime.LifetimeServices.LeaseManagerPollTime"/> property.
        /// </returns>
        /// <exception cref="T:System.Security.SecurityException">
        /// The immediate caller does not have infrastructure permission.
        /// </exception>
        /// <PermissionSet>
        /// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="RemotingConfiguration, Infrastructure"/>
        /// </PermissionSet>
        public override object InitializeLifetimeService()
        {
            return null;
        }

        /// <summary>
        /// Executes the test for class.
        /// </summary>
        /// <param name="testClass">The test class.</param>
        /// <returns></returns>
        public IDictionary<ITestElement, UnitTestResult> ExecuteTestForClass(List<UnitTest> testClass)
        {
            return _testController.ExecuteTestForClass(testClass);
        }

        /// <summary>
        /// Cancels the current run.
        /// </summary>
        public static void CancelRun()
        {
            TestLoaderExecutionEventDispatcher.CancelTests();
        }

        /// <summary>
        /// Cleanups this instance.
        /// </summary>
        public void Cleanup()
        {
            _testController.Cleanup();
        }

        #region IDisposable
        private bool _disposed;
        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            if (!(this._disposed))
            {
                if (disposing)
                {
                    _testController.Dispose();
                }
            }
            this._disposed = true;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="TestLoaderExecutionHandler"/> is reclaimed by garbage collection.
        /// </summary>
        ~TestLoaderExecutionHandler()
        {
            Dispose(false);
        }
        #endregion

    }
}
